/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.landmark;

import java.io.FileWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.JOptionPane;
//import javax.lang.model.element.Element;
//import javax.lang.model.util.Elements;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
//import javax.swing.text.Document;
import org.htmlparser.Node;
//import javax.xml.soap.Node;
import org.htmlparser.Parser;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author computer
 */
public class BodekCat extends SwingWorker<Boolean, Boolean> {

    /**
     * @param args
     */
    public void start() {
        try {

            String dirName = "./outputs/BodekImages";
            
            Util.createDirectory(dirName);

            txtArea = Util.createOutputFile("Bodek");
            fw = new FileWriter(Util.getHelperBean().getOutputFile());
            fwErrorFile = new FileWriter(Util.getHelperBean().getErrorFile());
            HelperBean helperBean = Util.getHelperBean();
            
            
            String[] categories = {"2", "W", "3", "1", "4", "6", "7", "ot", "G35", "N"};
            // Process the all products page to get the main category links which are on the left side menu of the page            
            for (String cat : categories) {
                if (Util.getHelperBean().isTerminateCurrentProcess()) {
                    break;
                }
                
                String productsUrl = "http://www.bodekandrhodes.com/cgi-bin/barlive/site.w?location=olc/catalog-browse.w&category=" + cat + "&page=all&target=main&frames=no&sponsor=000001";
               
                txtArea.setText("Processing page: " + productsUrl + "\n");
                txtArea.paintImmediately(txtArea.getVisibleRect());
                
                processHomePageLinks(productsUrl);

            }
            Enumeration modelEnum = htModelLinks.keys();
            
            txtArea.append("\nFiles are located at : " + helperBean.getOutputFile().getAbsolutePath() + "\n" + helperBean.getErrorFile().getAbsolutePath()+"\n");                
            txtArea.append("Total products : " + htModelLinks.size() + "\n");
            txtArea.paintImmediately(txtArea.getVisibleRect());
            
            while (modelEnum.hasMoreElements()) {
                if (Util.getHelperBean().isTerminateCurrentProcess()) {
                    break;
                }

                sb = new StringBuffer();
                String key = (String) modelEnum.nextElement();
//
                txtArea.append("Processing page: " + key + "\n");
                txtArea.paintImmediately(txtArea.getVisibleRect());

                fw.write("\n");
//                //processModel("http://www.fenixlighting.com/products/hp10.html");
                if (pageNo <= 3) {
                    processModel(htModelLinks.get(key));
                    pageNo++;
                }
//
//                String str = sb.toString();
//                str = str.replaceAll("\r", "")
//                         .replaceAll("\n", "")
//                         .replaceAll("   ", "")
//                         .replaceAll("<tr>", "\n<tr>")
//                         .replaceAll(",", "&#44;");
//                         
//                         
//                fw.write(str);
//                fw.flush();
            }
//
//
//            if (fw != null) {
//                fw.close();
//            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (Exception e1) {
                // TODO: handle exception
            }
        }
        status = "Done processing.";
        if (Util.getHelperBean().isTerminateCurrentProcess()) {
            status = "Process Terminated";
        }
        txtArea.append("\n" + status);
    }

    @Override
    protected Boolean doInBackground() throws Exception {
        start();
        return true;
    }

    @Override
    protected void done() {
        JOptionPane.showMessageDialog(Util.getHelperBean().getDataPanel(), status);
        Util.getHelperBean().setTerminateCurrentProcess(false);
        Util.getHelperBean().getToggleButton().setEnabled(false);
    }

    /**
     *
     * @param doc
     */
    private void loadAndSaveImagesFromPage(Document doc) {
        Elements elements = doc.getElementById("sim2").select("img");
        try{
        for (Element e : elements) {
            Util.saveImage(e.attr("src"), "./outputs/BodekImages", null);
        }
        }catch( Exception e ) {
            
        }
    }

    private void processModel(String aUrl) {
        try {
            Document doc = Util.getDocumentFromUrl(aUrl);            
            Parser parser = Util.getParserFromUrl(aUrl, doc);
            
            loadAndSaveImagesFromPage(doc);
            
            // to know which URL/model we have the data for in the final output/file
            fw.write(aUrl + "\n");

//            NodeList nl = parser.parse(new HasAttributeFilter("id", "sim2"));
//            System.out.println("Image: " + nl.size());
//            nl.extractAllNodesThatMatch("img")
//            //NodeList tableNodes = nl.extractAllNodesThatMatch(new HasAttributeFilter("class", "tab-content"), true);
//
//            for (int i = 0; i < nl.size(); i++) {
//                Node targetTableNode = nl.elementAt(i);// retrives the table content where the description starts which we are intersetd in.			
//                System.out.println(targetTableNode.getText());
//                String nodeData = 
//                String imageUrl = targetTableNode.
//                getImage(aUrl);
//               // iterateTag(targetTableNode);
//            }
        } catch (Exception e) {
            try {
                fwErrorFile.write(aUrl + " \n" + e.getMessage() + " \n");
                fwErrorFile.flush();
                e.printStackTrace();
            } catch (Exception e1) {
                // TODO: handle exception
            }
        }
    }

    

    private void iterateTag(Node aNode) throws Exception {
        if (aNode instanceof TextNode) {
            // downcast to TextNode
            TextNode text = (TextNode) aNode;

            sb.append(text.getText());// manipulating for table data which contains commas in one column
        } else if (aNode instanceof TagNode) {
            // downcast to TagNode
            TagNode tag = (TagNode) aNode;
            if (tag.getRawTagName().equalsIgnoreCase("li")) {
                sb.append("<li>");
            }
            if (tag.getRawTagName().equalsIgnoreCase("p")) {
                sb.append("<p>");
            }
            if (tag.getRawTagName().equalsIgnoreCase("br")) {
                sb.append("<br>");
            }
            if (tag.getRawTagName().equalsIgnoreCase("tr")) {
                sb.append("<tr>");
            }
            if (tag.getRawTagName().equalsIgnoreCase("td")) {
                sb.append("<td>");
            }
            // process recursively (nodes within nodes) via getChildren()				
            NodeList nl = tag.getChildren();
            if (null != nl) {
                try {
                    for (NodeIterator i = nl.elements(); i.hasMoreNodes();) {
                        iterateTag(i.nextNode());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void processHomePageLinks(String aUrl) {
        try {
            Document doc = Jsoup.connect(aUrl).get();
            Elements catElements = doc.getElementsByClass("tdBorder").select("a[href]");
            for (Element element : catElements) {
                String str = element.attr("href").replace("?", "&");
                str = "http://www.bodekandrhodes.com/cgi-bin/barlive/site.w?location=" + str.substring(str.indexOf("'") + 1, str.indexOf("',")) + "&frames=no&target=main&sponsor=000001&nocache=";
                htModelLinks.put(str, str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    FileWriter fw = null;
    int pageNo = 0;
    FileWriter fwErrorFile = null;
    StringBuffer sb = new StringBuffer();
    Hashtable<String, String> htModelLinks = new Hashtable<String, String>();
    Hashtable<String, String> htCatLinks = new Hashtable<String, String>();
    JTextArea txtArea;
    String status = "";
}
